//
//  TCLYPayViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/13.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/*******************************************************************这个是选择支付方式的界面 支付宝 微信 钱包支付方式*********************************************************************************************/

#import "TCLYPayViewController.h"
#import "TCLYPaySucceedViewController.h"
#import "TCLYPaySucceedViewController.h"
#import "TCLYHomeModel.h"
#import "TCLYRegisterView.h"
#import "Macros.h"
#import "Masonry.h"


@interface TCLYPayViewController ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewLayout;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIView *walletView;
@property (weak, nonatomic) IBOutlet UIView *alipay;
@property (weak, nonatomic) IBOutlet UIView *wechat;
@property (weak, nonatomic) IBOutlet UIButton *walletBtn;
@property (weak, nonatomic) IBOutlet UIButton *alipayBtn;
@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UIImageView *walletImageView;
@property (weak, nonatomic) IBOutlet UIImageView *alipayImageView;
@property (weak, nonatomic) IBOutlet UIImageView *wechatImageView;

@property (nonatomic, strong) TCLYHomeModel * homeModel;
@property (nonatomic, strong) TCLYRegisterView * registerView;

@end

@implementation TCLYPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * walletTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(walletTapAction)];
    [self.walletView addGestureRecognizer:walletTap];
    UITapGestureRecognizer * alipayTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alipayTapAction)];
    [self.alipay addGestureRecognizer:alipayTap];
    UITapGestureRecognizer * wechatTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wechatTapAction)];
    [self.wechat addGestureRecognizer:wechatTap];
    
    // Do any additional setup after loading the view from its nib.
    //钱包支付通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payForWallet) name:@"payForWallet" object:nil];
    //微信支付通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payForWechat) name:@"payForWechat" object:nil];
    self.homeModel = [[TCLYHomeModel alloc] init];
    self.registerView = [[TCLYRegisterView alloc] init];
    
    self.bgView.backgroundColor = bgColor;
    if (screenHeight >= 667.0) {
        self.scrollViewLayout.constant = 667.0-64-44;
    }else{
        self.scrollViewLayout.constant = 667.0;
    }
    [self createView];
    [self createTitleView];
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"付款详情";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popToRootViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = NO;
        }
            break;
        case 92:{
            
        }
            break;
    }
}

-(void)createView{

    self.priceLabel.text = self.payDic[@"sumPrice"];
    [self.walletBtn setImage:[UIImage imageNamed:@"mineTick-click"] forState:UIControlStateSelected];
    self.walletBtn.selected = YES;
    [self.alipayBtn setImage:[UIImage imageNamed:@"mineTick-click"] forState:UIControlStateSelected];
    self.alipayBtn.selected = NO;
    [self.wechatBtn setImage:[UIImage imageNamed:@"mineTick-click"] forState:UIControlStateSelected];
    self.wechatBtn.selected = NO;
    self.walletImageView.image = [UIImage imageNamed:@"wallet"];
    self.alipayImageView.image = [UIImage imageNamed:@"payAlipay"];
    self.wechatImageView.image = [UIImage imageNamed:@"payWechat"];
    
}

-(void)walletTapAction{
    self.walletBtn.selected = YES;
    self.alipayBtn.selected = NO;
    self.wechatBtn.selected = NO;
}
-(void)alipayTapAction{
    self.walletBtn.selected = NO;
    self.alipayBtn.selected = YES;
    self.wechatBtn.selected = NO;
}
-(void)wechatTapAction{
    self.walletBtn.selected = NO;
    self.alipayBtn.selected = NO;
    self.wechatBtn.selected = YES;
}
-(IBAction)payAction:(id)sender{
    if (self.walletBtn.selected == YES) {
        //这里是从钱包支付的判断
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.payDic[@"orderId"],@"order_id",@"1",@"pay_type", nil];
        [self.homeModel payForWalletParameter:dic];
        //从支付宝支付的判断
    }else if (self.alipayBtn.selected == YES){
        //从微信支付的判断
    }else if (self.wechatBtn.selected == YES){
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.payDic[@"orderId"],@"order_id",@"3",@"pay_type", nil];
        [self.homeModel payForWechatParameter:dic];
    }
}
//从钱包支付的一系列判断等
-(void)payForWallet{
    NSLog(@"%@",self.homeModel.wallectSucceed);
    if ([self.homeModel.wallectSucceed isEqual:@"钱包余额不足"]) {
        [self.registerView Prompt:@"您的钱包余额不足哦" Controller:self];
        self.registerView.determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            return ;
        }];
        self.registerView.cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            return;
        }];
        [self.registerView.promptAlertCtr addAction:self.registerView.determineAction];
        [self.registerView.promptAlertCtr addAction:self.registerView.cancelAction];
    }else{
        TCLYPaySucceedViewController * paySucceedViewCtr = [[TCLYPaySucceedViewController alloc] init];
        paySucceedViewCtr.parameterStr = self.payDic[@"orderId"];
        paySucceedViewCtr.isHome = self.isBaby;
//        NSLog(@"%@",paySucceedViewCtr.parameterStr);
        [self.navigationController pushViewController:paySucceedViewCtr animated:YES];
        self.navigationController.navigationBarHidden = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"paySucceedViewCtr" object:self];
        
    }

}
//从微信支付的一系列操作
-(void)payForWechat{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
